package com.coolwen.service.expert;

import com.coolwen.dao.ExpertMapper;
import com.coolwen.entity.Expert;
import com.coolwen.entity.Pager;
import com.coolwen.entity.SystemContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by nieyu on 2016/8/8.
 */
@Service("expertService")
public class ExpertServiceImpl implements ExpertService{

    @Autowired
    private ExpertMapper expertMapper;

    @Override
    public int insertSelective(Expert expert) {
        return expertMapper.insertSelective(expert);
    }

    @Override
    public List<Expert> selectAllInfo() {
        return expertMapper.selectAllInfo();
    }

    @Override
    public Pager<Expert> selectExpertCourseInfo(String grade, String department, String major, String course) {

        int pageSize = SystemContext.getPageSize();
        int pageOffset = SystemContext.getPageOffset();

        Pager<Expert> pages = new Pager<Expert>();
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {
            params.put("grade", "%" + grade + "%");

        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        if (course != null && !course.equals("")) {
            params.put("course", "%" + course + "%");
        }
        params.put("pageSize",  pageSize);
        params.put("pageOffset", pageOffset);

        List<Expert> datas = expertMapper.selectExpertCourseInfo(params);

        int totalRecord = expertMapper.find_count(params);
        pages.setDatas(datas);
        pages.setPageOffset(pageOffset);
        pages.setPageSize(pageSize);
        pages.setTotalPage(totalRecord);

        return pages;
    }

    @Override
    public Pager<Expert> selectAllExpertCourseInfo(String grade, String department, String major, String course) {

        int pageSize = SystemContext.getPageSize();
        int pageOffset = SystemContext.getPageOffset();

        List<Integer> grades = new ArrayList<Integer>();

        Pager<Expert> pages = new Pager<Expert>();
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {

            int g1 = Integer.valueOf(grade);
            int g2 = g1 + 1;
            int g3 = g1 + 2;
            int g4 = g1 + 3;
            grades.add(g1);
            grades.add(g2);
            grades.add(g3);
            grades.add(g4);
            params.put("grade", grades);
        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        if (course != null && !course.equals("")) {
            params.put("course", "%" + course + "%");
        }
        params.put("pageSize",  pageSize);
        params.put("pageOffset", pageOffset);

        List<Expert> datas = expertMapper.selectAllExpertCourseInfo(params);

        int totalRecord = expertMapper.selectCourse_count(params);
        pages.setDatas(datas);
        pages.setPageOffset(pageOffset);
        pages.setPageSize(pageSize);
        pages.setTotalPage(totalRecord);

        return pages;
    }

}
